<?php

namespace app\admin\controller\projects\music;

use app\Base;
use app\service\admin\projects\music\SongService;
use app\service\UploadServices;
use app\validate\admin\SongValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

class Song extends Base
{
    /**
     * @param UploadServices $services
     * @return View
     */
    public function show(UploadServices $services): View
    {
        $token = $services::getToken();
        return view('projects/music/list', ['token' => $token]);
    }

    /**
     * 列表
     * @param SongService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList(SongService $service): Json
    {
        $res = $service::getList($this->params);
        $this->res['count'] = $res['count'];
        $this->res['data'] = $res['data'];
        return $this->returnData();
    }

    /**
     * 添加
     * @param SongService $service
     * @param SongValidate $validate
     * @return Json
     */
    public function add(SongService $service, SongValidate $validate): Json
    {
        $res = $validate->scene('add')->check($this->params);
        if (!$res) {
            return $this->returnData('fail', $validate->getError());
        }
        $res = $service::add($this->params);
        if (!$res) {
            return $this->returnData('addFail');
        }
        return $this->returnData();
    }

    /**
     * 修改
     * @param SongService $service
     * @param SongValidate $validate
     * @return Json
     * @throws DbException
     */
    public function update(SongService $service, SongValidate $validate): Json
    {
        $res = $validate->scene('update')->check($this->params);
        if (!$res) {
            return $this->returnData('fail', $validate::getError());
        }
        $res = $service::update($this->params);
        if (!$res) {
            return $this->returnData('updateFail');
        }
        return $this->returnData();
    }

    /**
     * 删除
     * @param SongService $service
     * @return Json
     * @throws DbException
     */
    public function delete(SongService $service): Json
    {
        $res = $service::delete($this->params);
        if (!$res) {
            return $this->returnData('deleteFail');
        }
        return $this->returnData();
    }
}